package com.diandian.backend.kuaishou.life.task.service;

import com.diandian.backend.kuaishou.life.task.dto.*;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.kuaishou.life.task.domain.KuaishouLifeToken;
import com.diandian.backend.kuaishou.life.task.mapper.KuaishouLifeTokenMapper;
import com.diandian.backend.kuaishou.life.task.resp.KuaishouLifeTokenDetailResp;
import com.diandian.backend.kuaishou.life.task.resp.KuaishouLifeTokenPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.kuaishou.life.task.resp.KuaishouLifeTokenListResp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BusinessKuaishouLifeTokenService {

    @Autowired
    private KuaishouLifeTokenMapper kuaishouLifeTokenMapper;

    public PageResult page(KuaishouLifeTokenPageParamDTO requestDTO){

        requestDTO.check();

        // 查询总数
        int totalCount = kuaishouLifeTokenMapper.getCount(requestDTO);
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询数据集合
        List<KuaishouLifeToken> dataList = kuaishouLifeTokenMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<KuaishouLifeTokenPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                KuaishouLifeTokenPageResp item = new KuaishouLifeTokenPageResp();
                item.setId(x.getId());
                item.setUserId(x.getUserId());
                item.setMerchantId(x.getMerchantId());
                item.setAppKey(x.getAppKey());
                item.setSourceTokenData(x.getSourceTokenData());
                item.setAccessToken(x.getAccessToken());
                item.setRefreshToken(x.getRefreshToken());
                item.setOpenId(x.getOpenId());
                item.setExpiresIn(x.getExpiresIn());
                item.setExpiresDate(x.getExpiresDate());
                item.setScopes(x.getScopes());
                item.setCreateTime(x.getCreateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<KuaishouLifeTokenListResp> getList(KuaishouLifeTokenListParamDTO requestDTO){

        // 查询数据集合
        List<KuaishouLifeToken> dataList = kuaishouLifeTokenMapper.getList(requestDTO);

        // 组装数据
        List<KuaishouLifeTokenListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                KuaishouLifeTokenListResp item = new KuaishouLifeTokenListResp();
                item.setId(x.getId());
                item.setUserId(x.getUserId());
                item.setMerchantId(x.getMerchantId());
                item.setAppKey(x.getAppKey());
                item.setSourceTokenData(x.getSourceTokenData());
                item.setAccessToken(x.getAccessToken());
                item.setRefreshToken(x.getRefreshToken());
                item.setOpenId(x.getOpenId());
                item.setExpiresIn(x.getExpiresIn());
                item.setExpiresDate(x.getExpiresDate());
                item.setExpiresDateRefreshToken(x.getExpiresDateRefreshToken());
                item.setScopes(x.getScopes());
                item.setCreateTime(x.getCreateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public KuaishouLifeTokenDetailResp getDetail(KuaishouLifeTokenDetailParamDTO param) {

        KuaishouLifeToken dbData = kuaishouLifeTokenMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        KuaishouLifeTokenDetailResp result = new KuaishouLifeTokenDetailResp();
        result.setId(dbData.getId());
        result.setUserId(dbData.getUserId());
        result.setMerchantId(dbData.getMerchantId());
        result.setAppKey(dbData.getAppKey());
        result.setSourceTokenData(dbData.getSourceTokenData());
        result.setAccessToken(dbData.getAccessToken());
        result.setRefreshToken(dbData.getRefreshToken());
        result.setOpenId(dbData.getOpenId());
        result.setExpiresIn(dbData.getExpiresIn());
        result.setExpiresDate(dbData.getExpiresDate());
        result.setExpiresDateRefreshToken(dbData.getExpiresDateRefreshToken());
        result.setScopes(dbData.getScopes());
        result.setCreateTime(dbData.getCreateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(KuaishouLifeTokenUpdateByIdParamDTO param) {
        param.check();

        KuaishouLifeTokenDetailParamDTO queryDetailDTO = new KuaishouLifeTokenDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        KuaishouLifeToken dbData = kuaishouLifeTokenMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        kuaishouLifeTokenMapper.updateById(param);
    }

    public void deleteById(KuaishouLifeTokenDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        kuaishouLifeTokenMapper.deleteById(paramDTO);
    }

    public void deleteByAppKeyAndMerchantId(String appKey, String merchantId) {
        kuaishouLifeTokenMapper.deleteByAppKeyAndMerchantId(appKey, merchantId);
    }

    public void add(KuaishouLifeTokenAddParamDTO param) {
        param.setCreateTime(new Date());
        param.setDel(0);
        kuaishouLifeTokenMapper.add(param);
    }
}
